<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * RDF
 * 
 * Shared code for RDF documents and resources.
 */
abstract class simplerdf_Rdf
{
	//TODO: move these to the config file instead?
	// a few common namespace prefixes to make the result RDF documents more readable
	static $nsPrefixes = array(
			'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
			'foaf' => 'http://xmlns.com/foaf/0.1/',
			'dc' => 'http://purl.org/dc/elements/1.1/',
			'dcterms' => 'http://purl.org/dc/terms/',
			'fedora-rels-ext' => 'info:fedora/fedora-system:def/relations-external#',
			'fedora-model' => 'info:fedora/fedora-system:def/model#',
			'fedora-view' => 'info:fedora/fedora-system:def/view#',
			'ore' => 'http://www.openarchives.org/ore/terms/',
			'owl' => 'http://www.w3.org/2002/07/owl#',
			'escape-system' => 'http://purl.utwente.nl/ns/escape-system.owl#',
			'escape-display' => 'http://purl.utwente.nl/ns/escape-display.owl#',
			'escape-agents' => 'http://purl.utwente.nl/ns/escape-agents.owl#',
			'escape-annotations' => 'http://purl.utwente.nl/ns/escape-annotations.owl#',
			'escape-discourserelationships' => 'http://purl.utwente.nl/ns/escape-discourserelationships.owl#',
			'escape-events' => 'http://purl.utwente.nl/ns/escape-events.owl#',
			'escape-projects' => 'http://purl.utwente.nl/ns/escape-projects.owl#',
			'escape-pubtypes' => 'http://purl.utwente.nl/ns/escape-pubtypes.owl#',
			'sw-vocab-status' => 'http://www.w3.org/2003/06/sw-vocab-status/',
			'skos' => 'http://www.w3.org/2004/02/skos/core#',
			'swan' => 'http://swan.mindinformatics.org/ontologies/1.2/discourse-relationships/',
			'swrc' => 'http://swrc.ontoware.org/ontology#',
	);
	
	/**
	 * Get a new instance of the RDF/XML serializer
	 * @return ARC2_RDFSerializer RDF/XML serializer
	 */
	protected function getRdfXmlSerializer()
	{
		return ARC2::getRDFXMLSerializer(array('ns' => simplerdf_Rdf::$nsPrefixes));
	}
	
	/**
	 * Get a new instance of the RDF/XML parser
	 * @return ARC2_RDFParser RDF/XML parser
	 */
	protected function getRdfXmlParser()
	{
		return ARC2::getRDFXMLParser(array('ns' => simplerdf_Rdf::$nsPrefixes));
	}
	
	/**
	 * Get a new instance of the RDF/JSON serializer
	 * @return ARC2_RDFSerializer RDF/JSON serializer
	 */
	protected function getRdfJsonSerializer()
	{
		return ARC2::getRDFJSONSerializer(array('ns' => simplerdf_Rdf::$nsPrefixes));
	}

	/**
	 * Get a new instance of the RDF/JSON parser
	 * @return ARC2_RDFParser RDF/JSON parser
	 */
	protected function getRdfJsonParser()
	{
		return ARC2::getRDFJSONParser(array('ns' => simplerdf_Rdf::$nsPrefixes));
	}
	
	/**
	 * Convert an ARC2 SimpleIndex with flattened objects to an ARC2 SimpleIndex with typed objects.
	 * Object types will be guessed using regex patterns since SimpleRDF uses flattened object indexes internally.
	 * @param array $flatSimpleIndex ARC2 SimpleIndex with flattened objects
	 * @return array ARC2 SimpleIndex
	 */
	protected function convertFlatSimpleIndexToSimpleIndex($flatSimpleIndex)
	{
		$simpleIndex = array();
		foreach($flatSimpleIndex as $subject => $predicateArray)
		{
			foreach($predicateArray as $predicate => $objectArray)
			{
				foreach($objectArray as $object)
				{
					$typedObject = array();
					$typedObject['value'] = $object;
					$typedObject['type'] = simplerdf_Util::guessType($object);
					
					$simpleIndex[$subject][$predicate][] = $typedObject;
				}
			}
		}
		
		return $simpleIndex;
	}
	
} 